import mongoose from 'mongoose'
import { dirs } from '../tool'
import config from '../config'
import { resolve } from 'path'
dirs('./database/schema').forEach(f => require(resolve(
  __dirname,
  '../database/schema',
  f
)))
export const database = app => {
  mongoose.set('debug', true)
  mongoose.connect(config.db)

  mongoose.connection.on('disconnected', () => {
    mongoose.connect(config.db)
  })
  mongoose.connection.on('error', err => {
    console.error(err)
  })
  mongoose.connection.on('open', cb => {
    console.log('Connected to MongoDB', config.db)
  })
}
